/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.dsw2.biblioteca8a.Dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import mx.itschapala.dsw2.biblioteca8a.modelo.Libro;

/**
 *
 * @author Raven
 */
@Stateless
public class LibroDao extends AbstractDao<Libro> implements LibroDaoLocal {
    @PersistenceContext(unitName = "biblioteca8amy-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public LibroDao() {
        super(Libro.class);
    }
    
        @Override
     public Libro find(Object id) {
        return new Libro();
    }
        
    @Override
        public Libro findByClave(Libro l)
        {
        Query q=em.createNamedQuery("Libro.findByClave");
                q.setParameter("clave", l.getClave());
              if(q.getResultList().isEmpty()){
                return null;
              }  else {return (Libro) q.getResultList().get(0);}
        }

    @Override
    public boolean findDuplicity(Libro book) {
         //seleccionar consulta
        Query q= em.createNamedQuery("Libro.findDuplicity");
        //poner parametros
        q.setParameter("clave",book.getClave());
        q.setParameter("nombre",book.getNombre());
        //ejecutar
        return !q.getResultList().isEmpty(); }
}
